<?php
/**
 * @author Radovan Dvorsky
 * @version 20100604
 */
require_once ('Zend/Form.php');

class Form_Harmonogram_Add extends Zend_Form {
		
	protected $_commissions;	
	protected $_years;
	
	public function init(){
		$cache = Zend_Registry::get('cache');		
		$cacheNameCommissions = My_Support::getCacheName('CommissionController','fetchAll');
		$cacheNameYears = My_Support::getCacheName($this,'fetchAll');
		
		if(!($this->_years = $cache->load($cacheNameYears))){
			$skRokModel = new Db_Mapper_SkRok();
			$this->_years = $skRokModel->fetchAll();
			$cache->save($this->_years,$cacheNameYears);
		}
		
		if(!($this->_commissions = $cache->load($cacheNameCommissions))){
			$commissionMapper = new Db_Mapper_Commission();
			$this->_commissions = $commissionMapper->fetchAll();
			$cache->save($this->_commissions,$cacheNameCommissions);
		}
	}
	
	public function __construct($options = null) {
		parent::__construct ($options);
		
		$this->addDecorator('Fieldset',array('legend' => 'Zostaviť harmonogram'));
		
		$urlHelper = new Zend_View_Helper_Url();
		$groupFillurl = $urlHelper->url(array(
			'controller' => 'harmonogram',
			'action' => 'fill-group',
			'format' => 'html'
			)
		);
		
		$studentFillUrl = $urlHelper->url(array(
			'controller' => 'harmonogram',
			'action' => 'fill-student',
			'format' => 'html'
			)
		);
		
		$onYearSelectChangeFunction = '$.post("' . $groupFillurl . '",{ID_sk: $(this).val()},' 
									   . 'function(data){$(".message").html(data);'
									   .	'$("select#ID_st").html("");'
									   . 	'$("select#ID_tr").html(data);'
									   . '},"html");';
		
		$onGroupSelectChangeFunction = '$.post("' . $studentFillUrl . '",{ID_tr: $(this).val(),ID_sk: $("select#yearSelect").val()},' 
									   . 'function(data){$(".message").html(data);'
									   . 	'$("select#ID_st").html(data);'
									   . '},"html");';
		
									   
		$yearSelect = new Zend_Form_Element_Select('yearSelect'); 
		$yearSelect->setLabel('Ročník:')
		->addMultiOption('-','Vyberte si:');
		$yearSelect->setAttrib('onchange',$onYearSelectChangeFunction);

		foreach ($this->_years as $year){
			$yearSelect->addMultiOption($year->ID_sk,$year->skolni_rok);
		}
				
		$groupSelect = new Zend_Form_Element_Select('ID_tr');
		$groupSelect->setLabel('Trieda:');
		$groupSelect->setAttrib('onchange',$onGroupSelectChangeFunction)
		->setRegisterInArrayValidator(false);

		
		$studentSelect = new Zend_Form_Element_Select('ID_st');
		$studentSelect->setLabel('Meno skúšaného:')
		->setRegisterInArrayValidator(false)
		->setAllowEmpty(false);
		
		
		$commissionObhajobaSelect = new Zend_Form_Element_Select('cidBc');
		$commissionObhajobaSelect->setLabel('Komisia obhajoba: ');
		$commissionObhajobaSelect->addMultiOption('','Vyberte si:');
		foreach($this->_commissions as $commission){
			$commissionObhajobaSelect->addMultiOption($commission->cid,$commission->name);
		}
		
		$commissionFirstBlockSelect = new Zend_Form_Element_Select('cidBlock1');
		$commissionFirstBlockSelect->setLabel('Komisia 1. odborný blok: ');
		$commissionFirstBlockSelect->addMultiOption('','Vyberte si:');
		foreach($this->_commissions as $commission){
			$commissionFirstBlockSelect->addMultiOption($commission->cid,$commission->name);
		}
		
		$commissionSecondBlockSelect = new Zend_Form_Element_Select('cidBlock2');
		$commissionSecondBlockSelect->setLabel('Komisia 2. odborný blok (nepovinné): ');
		$commissionSecondBlockSelect->addMultiOption('','Vyberte si:');
		foreach($this->_commissions as $commission){
			$commissionSecondBlockSelect->addMultiOption($commission->cid,$commission->name);
		}
		$commissionSecondBlockSelect->setAllowEmpty(true)->setRequired(false);
						
		$jqueryParams = array(
			'dateFormat' => 'dd-mm-yy',
			'dayNames' => array(
				'Nedeľa', 'Pondelok', 'Utorok', 'Streda', 'Štvrtok', 'Piatok', 'Sobota'
			),
			'dayNamesMin' => array(
				'Ne', 'Po', 'Ut', 'St', 'Št', 'Pi', 'So'
			),
			'duration' => 'fast',
			'firstDay' => 1,
			'monthNames' => array(
				'Január','Február','Marec','Apríl','Máj','Jún','Júl','August','September','Október','November','December'
			)
		);
		
		$date = new ZendX_JQuery_Form_Element_DatePicker("date");
		$date->setLabel('Dátum skúšky:')
		->setJqueryParams($jqueryParams);
		
		$block1 = new ZendX_JQuery_Form_Element_DatePicker("block1");
		$block1->setLabel('Čas 1. odborný blok:')
		->setJqueryParams($jqueryParams);
		
		$block1Time = new Zend_Form_Element_Text('block1Time');
		$block1Time->setAttrib('class','timepickr');
		
		
		$block2 = new ZendX_JQuery_Form_Element_DatePicker("block2");
		$block2->setLabel('Čas 2. odborný blok:')
		->setJqueryParams($jqueryParams);
		
		$bcdate = new ZendX_JQuery_Form_Element_DatePicker("bcdate");
		$bcdate->setLabel('Čas obhajoba:')
		->setJqueryParams($jqueryParams);
		
				
		$this->addElements(array(
			$yearSelect,
			$groupSelect,
			$studentSelect,
			$commissionObhajobaSelect,
			$commissionFirstBlockSelect,
			$commissionSecondBlockSelect,
			$date,
			//$block1,
			//$block2,
			//$bcdate
			)
		);	
	}
}